<?php
/**
 * 栏目管理模块
 * 
 + ----------------------------------------------------- 
 * Date 2013-3-8
 + -----------------------------------------------------
 */
class MenuAction extends CommonAction {
	
	public function __construct() {
		parent::__construct();
		$this->model=M("Menu");
	}
	
	public function index() {
		$pid=isset($_REQUEST['pid']) ? intval($_REQUEST['pid']) : 0 ;
        //生成查询Map对象，自定义模糊查询
        $map = $this->_mysearch(array('name'));
		
        $menus_html = $this->getMenu($pid);
		$this->assign('menus_html',$menus_html);
		
        $this->display();
    }
	
	public function add(){
		$pid=isset($_REQUEST['pid']) ? intval($_REQUEST['pid']) : 0 ;
		// 类型
		$type_model=M('MenuType');
		$types=$type_model->where('status=1')->order('sort_order asc')->getField('id,name');
		$this->assign('types',$types);
		// 所有父级栏目
		$all_menu=$this->model->where('parent_id=0')->order('sort_order asc')->select();
		$this->assign('all_menu',$all_menu);
		$this->display();
	}
	
	
	public function edit(){
		$id=isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0 ;
		$pid=isset($_REQUEST['pid']) ? intval($_REQUEST['pid']) : 0 ;
		
		$type_model=M('MenuType');
		if($pid){
			$pmenu=$this->model->find($pid);
			$this->assign('pmenu',$pmenu);
		}
		if($id){
			$data=$this->model->find($id);
			$this->assign('data',$data);
		}
		// 类型
		$type_model=M('MenuType');
		$types=$type_model->where('status=1')->order('sort_order asc')->getField('id,name');
		$this->assign('types',$types);
		//所有父级栏目
		$all_menu=$this->model->where('parent_id=0')->order('sort_order asc')->select();
		$this->assign('all_menu',$all_menu);
		$this->display('add');
	}
	
	public function ajax_delete() {
        //删除指定记录  ajax需要在方法中实例化类
		$id = $_REQUEST['id'];
		if (isset($id)) {
			$pids=array_filter(@explode(',', $id),"array_none");
			$pids_filt=array();
			foreach($pids as $v){
				if(!$this->check_sub_menu($v)){
					$pids_filt[]=$v;
				}
			}
			if(count($pids_filt)>0){
				//$ids=$this->get_sub_menu_ids(array(),$pids_filt);
				//$this->ajaxReturn(1,implode(',',$pids_filt).'删除成功！',1);
				$condition = array('id' => array('in', $pids_filt));
				if (false !== $this->model->where($condition)->delete()) {
					//删除附件
					$Attach=M("Attach");
					$condition=array(
							'record_id' =>	array('in', $pids_filt),
							'model'		=>	MODULE_NAME
					);
					$images=$Attach->where($condition)->select();
					if(count($images)>0){
						foreach ($images as $v){
							@unlink($v['path']);
						}
					}
					$Attach->where($condition)->delete();
					
					$this->ajaxReturn(1,'删除成功！',1);
				}else{
					 $this->ajaxReturn(0,'删除失败！',0);
				}
			}else{
				$this->ajaxReturn(0,'删除失败，请先删除下级栏目！',0);	
			}
		}else{
			 $this->ajaxReturn(0,'非法操作！',0);
		}
	}
	
	//判断是否有下级栏目
	public function check_sub_menu($parent_id){
		$has=false;
		if($parent_id){
			$count=$this->model->where("parent_id=".$parent_id)->count("id");
			if($count>0){
				$has=true;
			}
		}
		return $has;
	}
	
	/**
      +----------------------------------------------------------
     * 关联查询栏目
	 * @param parent_id  父级ID
	 * @param status  状态 1可用 0不可用
	 * @param order 排序
	  +----------------------------------------------------------
     */
	protected function getMenu($seek=0,$parent_id=0,$status=0,$order=''){
		$where=isset($status)&&$status==1 ? 'and m.status=1' :'' ;
		
		$menu=D('Menu');
		$menus=$menu->table(C("DB_PREFIX")."menu m")
			->join("left join ".C("DB_PREFIX")."menu_type t on m.type_id=t.id")
			->field("m.*,t.name,t.model")
			->where("m.parent_id=$parent_id $where")
			->order("m.sort_order asc")
			->select();
		$all_menus=array();
		$html=array();
		if(is_array($menus)&&count($menus)>0){
			foreach($menus as $k=>$v){
				if(!strpos($v['path'],'-')){
					$seek=0;
					$del='';
				}else{
					$array=@explode('-',$v['path']);
					$seek=(count($array)-1)*12;
					$class=array('show');    // class
					$class[]="next_".$v['parent_id'];
					foreach($array as $c_id){
						if($c_id){
							$class[]='child_'.$c_id;
						}
					}
					$class_str=implode(' ',$class);
					$del='<a href="javascript:;" ID="'.$v['id'].'" ACT="ajax_delete" onclick="return operation_one($(this));" class="list_table_a" title="删除">删除</a>';
				}
				// gif
				$m_pre='<img src="../Public/images/m_less.gif" class="m_more" pid="'.$v['id'].'" />';
				$img=$v['status']==1 ? '../Public/images/yes.gif' : '../Public/images/no.gif';
				$menu_keyword=empty($v['menu_keyword']) ? 'N/A' : $v['menu_keyword'];
				$menu_name_en=empty($v['menu_name_en']) ? 'N/A' : $v['menu_name_en'];
				$sub_add = !$seek ? '<a href="__URL__/add/pid/'.$v['id'].'" class="list_table_a">添加子类</a>' : '';
				$html[]='<tr class="'.$class_str.'">
							<td style="text-indent:5px;"><input type="checkbox" value="'.$v['id'].'" name="cd_id[]" /></td>
							<td style="text-align:center;">'.$v['id'].'</td>
							<td align="left" valign="middle" class="list_table_a" style="font-size:13px; text-indent:'.$seek.'px;">'.$m_pre.$v['menu_name'].'</td>
							<td align="left"><span class="ajax_edit" onclick="return edit(this, \'menu_name_en\', '.$v['id'].')">'.$menu_name_en.'</span></td>
							<td style="text-align:center;">'.$v['name'].'</td>
							<td style="text-align:left;"><span class="ajax_edit" onclick="return edit(this, \'link_id\', '.$v['id'].', \'int\')">'.$v['link_id'].'</span></td>
							<td style="text-align:center;"><img class="cursor" src="'.$img.'" id="'.$v['id'].'" field="status" onclick="return change($(this));" ></td>
							<td style="text-align:center;"><span class="ajax_edit" style="margin:0px auto;" onclick="return edit(this, \'sort_order\', '.$v['id'].' , \'int\')">'.$v['sort_order'].'</span></td>
							<td align="right">
							
							<a href="__APP__/'.GROUP_NAME.'/Article/index/mid/'.$v['id'].'" class="list_table_a hide">内容管理</a>
							<a href="__URL__/edit/pid/'.$v['parent_id'].'/id/'.$v['id'].'" class="list_table_a" title="编辑">编辑</a>
							'.$del.'
							'.$sub_add.'	
						</tr>';
				$html[]=$this->getMenu($seek,$v['id'],$status,$order);
			}
		}		
		//echo $menu->getLastSql();exit;
		return implode('',$html);
		
	}
}

?>